package com.cmict.data.infra.controller.admin.message;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.security.core.util.SecurityFrameworkUtils;
import com.cmict.data.infra.controller.admin.message.vo.AnnouncementDTO;
import com.cmict.data.infra.dal.dataobject.message.Announcement;
import com.cmict.data.infra.controller.admin.message.vo.AnnouncementQO;
import com.cmict.data.infra.controller.admin.message.vo.AnnouncementPageVO;
import com.cmict.data.infra.service.message.AnnouncementService;
import com.cmict.data.infra.service.message.job.ScheduledMessageService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 公告信息
 *
 * @author hccake 2020-12-15 17:01:15
 */
@Api(tags = " 消息管理 - 普通消息")
@RestController
@Validated
@RequestMapping("/notify/announcement")
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;
    @Resource
    ScheduledMessageService scheduledMessageService;

    /**
     * 分页查询
     *
     * @param announcementQO 公告信息查询对象
     * @return R 通用返回体
     */
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermission('notify:announcement:read')")
    @Operation(summary = "分页查询", description = "分页查询")
    public CommonResult<PageResult<AnnouncementPageVO>> getAnnouncementPage(@Validated @RequestBody AnnouncementQO announcementQO) {
        return CommonResult.success(announcementService.queryPage(announcementQO));
    }

    /**
     * 新增公告信息
     *
     * @param announcementDTO 公告信息
     * @return R 通用返回体
     */
    @PostMapping
//    @PreAuthorize("@ss.hasPermission('notify:announcement:create')")
    @Operation(summary = "新增公告信息", description = "新增公告信息")
    public CommonResult<Boolean> save(@Valid @RequestBody AnnouncementDTO announcementDTO) {
        return announcementService.addAnnouncement(announcementDTO) ? CommonResult.success(true)
                : CommonResult.error(500, "新增公告信息失败");
    }

    /**
     * 修改公告信息
     *
     * @param announcementDTO 公告信息
     * @return R 通用返回体
     */

    @PutMapping
    @PreAuthorize("@ss.hasPermission('notify:announcement:edit')")
    @Operation(summary = "修改公告信息", description = "修改公告信息")
    public CommonResult<Boolean> updateById(@Valid @RequestBody AnnouncementDTO announcementDTO) {
        return announcementService.updateAnnouncement(announcementDTO) ? CommonResult.success(true)
                : CommonResult.error(500, "修改公告信息失败");
    }

    /**
     * 通过id删除公告信息
     *
     * @param id id
     * @return R 通用返回体
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('notify:announcement:del')")
    @Operation(summary = "通过id删除公告信息", description = "通过id删除公告信息")
    public CommonResult<Boolean> removeById(@PathVariable("id") Long id) {
        return announcementService.removeId(id) ? CommonResult.success(true)
                : CommonResult.error(500, "通过id删除公告信息失败");
    }


    /**
     * 撤回公告信息
     *
     * @return R 通用返回体
     */
    @PatchMapping("/close/{announcementId}")
    @PreAuthorize("@ss.hasPermission('notify:announcement:edit')")
    @Operation(summary = "撤回公告信息", description = "撤回公告信息")
    public CommonResult<Boolean> disableAnnouncement(@PathVariable("announcementId") Long announcementId) {
        return announcementService.retractId(announcementId) ? CommonResult.success(true)
                : CommonResult.error(500, "撤回公告信息");
    }


    /**
     * 用户未读公告
     *
     * @return
     */
    @GetMapping("/user")
    @PreAuthorize("@ss.hasPermission('notify:userannouncement:read')")
    @Operation(summary = "用户未读公告", description = "用户未读公告")
    public CommonResult<List<Announcement>> getUserAnnouncements() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return CommonResult.success(announcementService.listActiveAnnouncements(userId));
    }


    /**
     * 分页查询
     *
     * @param id 公告信息查询对象
     * @return R 通用返回体
     */
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('notify:announcement:read')")
    @Operation(summary = "通过id查询公告信息", description = "通过id查询公告信息")
    public CommonResult<Announcement> getAnnouncementPage(@PathVariable Long id) {
        return CommonResult.success(announcementService.getAnnouncement(id));
    }

    @GetMapping("/job/{id}")
    @Operation(summary = "通过id查询定时任务", description = "通过id查询定时任务")
    public CommonResult<JobDetail> getJob(@PathVariable Long id) {
        scheduledMessageService.getJob(id);
        return CommonResult.success(scheduledMessageService.getJob(id));
    }

    @GetMapping("/trigger/{id}")
    @Operation(summary = "通过id查询定时任务", description = "通过id查询定时任务")
    public CommonResult<List<? extends Trigger>> getTriggersOfJob(@PathVariable Long id) {
        return CommonResult.success(scheduledMessageService.getTriggersOfJob(id));
    }

    @PostMapping("/job")
    @Operation(summary = "通过id查询定时任务", description = "通过id查询定时任务")
    public CommonResult doJob(@RequestBody Integer id) {
        scheduledMessageService.triggerJob(id);
        return CommonResult.success(true);
    }
}